﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pro260Website.Models;
using Domain.Entity;
using System.IO;
using Domain.Concrete;
using Domain.Abstract;
using System.Web.Security;

namespace Pro260Website.Controllers
{
    public class VideoController : Controller
    {
        private IVideoRepository repository;
        private const int DEFAULT_PAGE_SIZE = 5;

        public VideoController(IVideoRepository videoRepository)
        {

            repository = videoRepository;

        }

        public ViewResult Listings(int page = 1, int pageSize = DEFAULT_PAGE_SIZE)
        {
            var viewModel = GetViewModel(page, pageSize);

            return View(viewModel);
        }

        [Authorize(Roles="Admin")]
        public ViewResult AdminListings(int page = 1, int pageSize = DEFAULT_PAGE_SIZE)
        {
            var viewModel = GetViewModel(page, pageSize);

            return View(viewModel);
        }

        [Authorize]
        public ActionResult UploadVideo()
        {
            return View();
        }

        [Authorize(Roles="Admin")]
        public ActionResult DeleteVideo(int videoID)
        {
            Video video = repository.Videos.FirstOrDefault(v => v.VideoID == videoID);

            if (video != null)
            {
                repository.RemoveVideo(video);
            }
            return RedirectToAction("AdminListings");
        }

        [HttpPost]
        [Authorize]
        public ActionResult UploadVideo(HttpPostedFileBase file)
        {

            if (file != null && file.ContentLength > 0)
            {
                var fileNameOriginal = Path.GetFileName(file.FileName);
                var pathTemp = Path.Combine(Server.MapPath(""), fileNameOriginal);
                var fileExtension = Path.GetExtension(pathTemp);
                if (fileExtension.Equals(".swf") || fileExtension.Equals(".wmv"))
                {
                    bool nameExists = false;
                    string ID = string.Empty;
                    while (true)
                    {
                        nameExists = false;
                        ID = GenerateID() + fileExtension;

                        foreach (var v in repository.Videos)
                        {
                            if (v.VideoName != null)
                            {
                                if (v.VideoName.Equals(ID))
                                {
                                    nameExists = true;
                                }
                            }
                        }
                        if (!nameExists)
                        {
                            break;
                        }
                    }
                    if (!nameExists)
                    {
                        var path = Path.Combine(Server.MapPath("~/Content/Video"), ID);
                        file.SaveAs(path);

                        Domain.Entity.Video video = new Video()
                        {
                            VideoName = ID,
                            OriginalName = fileNameOriginal,
                            ExtensionType = fileExtension
                        };

                        repository.SaveVideo(video);
                    }
                }
            }
            if (Roles.IsUserInRole("Admin"))
            {
                return RedirectToAction("AdminListings");
            }
            else
            {
                return RedirectToAction("Listings");
            }
        }

        private string GenerateID()
        {
            Random random = new Random();
            string ID = "";
            for (int i = 0; i < 16; i++)
            {
                int num = random.Next(48, 58);
                int upper = random.Next(65, 91);
                int lower = random.Next(97, 123);

                int which = random.Next(3);
                if (which == 0)
                {
                    ID = ID + (char)num;
                }
                else if (which == 1)
                {
                    ID = ID + (char)upper;
                }
                else if (which == 2)
                {
                    ID = ID + (char)lower;
                }
            }

            return ID;
        }

        private ListingViewModel<Video> GetViewModel(int page, int pageSize)
        {
            var items = repository.Videos;

            var viewModel = new ListingViewModel<Video>()
            {
                Items = items.OrderBy(v => v.VideoName).Skip((page - 1) * pageSize).Take(pageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = items.Count()
                },
                QueryInfo = null
            };

            return viewModel;
        }
    }
}
